<?php

namespace Admin\Controller;

class RegionController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
    }

    function index() {
        $count = D("Region")->count();
        $page = $this->page($count, 20);
        $list = D("Region")->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    /**
     *  添加管理员
     */
    function add() {
        $this->display();
    }

    /**
     *  添加用户信息
     */
    function add_post() {
        if (IS_POST) {

            $data['temperature_lower_limit'] = I('post.temperatureLowerLimit');
            $data['temperature_upper_limit'] = I('post.temperatureUpperLimit');
            $data['humidity_lower_limit'] = I('post.humidityLowerLimit');
            $data['humidity_upper_limit'] = I('post.humidityUpperLimit');
            $data['voltage_lower_limit'] = I('post.voltageLowerLimit');
            $data['voltage_upper_limit'] = I('post.voltageUpperLimit');
            $data['current_lower_limit'] = I('post.currentLowerLimit');
            $data['current_upper_limit'] = I('post.currentUpperLimit');
            $data['create_time'] = getCurrentTime();
            $data['region_name'] = I('post.regionName');

            D("Region")->add($data);

            $this->success("添加成功！", U('region/index'));
        }
    }

    /**
     *  编辑用户基本信息
     */
    function edit() {
        $id = I('get.id');

        $info = D("region")->where(array('region_id' => $id))->find();
        $this->assign('info', $info);
        $this->display();
    }

    /**
     *  编辑管理员
     */
    function edit_post() {
        if (IS_POST) {
            $id = I('post.id');

            $data = D("region")->where(array("region_id"=>$id))->find();

            $data['temperature_lower_limit'] = I('post.temperatureLowerLimit');
            $data['temperature_upper_limit'] = I('post.temperatureUpperLimit');
            $data['humidity_lower_limit'] = I('post.humidityLowerLimit');
            $data['humidity_upper_limit'] = I('post.humidityUpperLimit');
            $data['voltage_lower_limit'] = I('post.voltageLowerLimit');
            $data['voltage_upper_limit'] = I('post.voltageUpperLimit');
            $data['current_lower_limit'] = I('post.currentLowerLimit');
            $data['current_upper_limit'] = I('post.currentUpperLimit');
            $data['region_name'] = I('post.regionName');

            D("region")->where(array('region_id' => $id))->save($data);

            $this->success("更新成功！", U('region/index'));
        }
    }


}
